package com.css.fxfzysx.modules.ysxManagement.repository.impl;

import com.css.fxfzysx.modules.ysxManagement.entity.BdFactorEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.CnRatioEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.SrBaselineEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.CecMainRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class CecMainRepositoryImpl implements CecMainRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveCnRatio(CnRatioEntity it) {
        jdbcTemplate.update("insert into czt_communication_ratio(ccr_id,category,category_value,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getCcrId(), it.getCategory(), it.getCategoryValue(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public void updateCnRatio(CnRatioEntity it) {
        String sql = "update czt_communication_ratio set category= :category,category_value= :categoryValue,update_user= :updateUser,update_time= :updateTime,project_id= :projectId" +
                " where ccr_id= '"+it.getCcrId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<CnRatioEntity> getAllCnRatio() {
        String sql = "select * from czt_communication_ratio where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CnRatioEntity.class));
    }

    @Override
    public void deleteCnRatioById(String cnRatioIds) {
        String sql = "update czt_communication_ratio set del_flag = '1' where ccr_id=?";
        jdbcTemplate.update(sql, cnRatioIds);
    }

    @Override
    public void saveSrBaseline(SrBaselineEntity it) {
        jdbcTemplate.update("insert into czt_seism_ratio_baseline(csrb_id,site,site_value,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getCsrbId(), it.getSite(), it.getSiteValue(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public void updateSrBaseline(SrBaselineEntity it) {
        String sql = "update czt_seism_ratio_baseline set site= :site,site_value= :siteValue,update_user= :updateUser,update_time= :updateTime,project_id= :projectId" +
                " where csrb_id= '"+it.getCsrbId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<SrBaselineEntity> getAllSrBaseline() {
        String sql = "select * from czt_seism_ratio_baseline where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SrBaselineEntity.class));
    }

    @Override
    public void deleteSrBaselineById(String srBaselineId) {
        String sql = "update czt_seism_ratio_baseline set del_flag = '1' where csrb_id=?";
        jdbcTemplate.update(sql, srBaselineId);
    }

    @Override
    public void deleteCnRatioByProjectId(String arrangeId) {
        String sql = "update czt_communication_ratio set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }

    @Override
    public void deleteSrBaselineByProjectId(String arrangeId) {
        String sql = "update czt_seism_ratio_baseline set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }

    @Override
    public List<CnRatioEntity> getCnRatioById(String ccrId) {
        String sql = "select * from czt_communication_ratio where ccr_id ='"+ccrId+"'";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(CnRatioEntity.class));
    }

    @Override
    public List<SrBaselineEntity> getSrById(String csrbId) {
        String sql = "select * from czt_seism_ratio_baseline where csrb_id ='"+csrbId+"'";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(SrBaselineEntity.class));
    }
}
